package com.wp.controller.impl;
import com.wp.entity.SysRole;
import com.wp.entity.SysUser;
import com.wp.service.SysRoleService;
import com.wp.service.SysUserService;
import com.wp.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.wp.utils.Constants;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    // 用户
    @Resource
    private SysUserService sysUserService;
    // 角色
    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String userList(@RequestParam(value = "pageIndex", required = false) String pageIndex,
                           @RequestParam(value = "queryRealName", required = false) String queryRealName,
                           @RequestParam(value = "queryRoleId", required = false) Integer queryRoleId,
                           Model model, HttpSession session) throws Exception {
        //默认页码
        Integer currentPageNo = 1;
        //每页显示的记录数
        Integer pageSize = Constants.pageSize;

        //从请求的参数中获取pageIndex，并转换为当前页码
        if (pageIndex != null && !"".equals(pageIndex)) {
            currentPageNo = Integer.valueOf(pageIndex);
        }

        //获取总记录数
        Integer totalCount = sysUserService.findUserTotalCount(queryRealName, queryRoleId);
        //计算总页数
        PageSupport pageSupport = new PageSupport();
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);
        //总页数
        Integer totalPageCount = pageSupport.getTotalPageCount();
        //控制页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        //获取业务层中的用户列表
        List<SysUser> userList = sysUserService.findUserByPage(currentPageNo, pageSize, queryRealName, queryRoleId);
        List<SysRole> roleList = sysRoleService.findAllSysRole();
        SysUser sysUser = (SysUser) session.getAttribute("UserSession");
        model.addAttribute("userList", userList);
        model.addAttribute("roleList", roleList);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("queryRealName", queryRealName);
        model.addAttribute("queryRoleId", queryRoleId);
        model.addAttribute("sysuserid", sysUser.getId());
        //逻辑视图名
        return "sysUser/list";
    }

    // 跳转到jsp添加页面
    @RequestMapping("/toAdd")
    public String toAdd(@ModelAttribute("sysUser") SysUser sysUser){

        return "sysUser/add";
    }

    // 查询出角色roleList
    @GetMapping(value = "/simpleList")
    @ResponseBody
    public List<SysRole> sysRoles() throws Exception {

        List<SysRole> roleList = sysRoleService.findAllSysRole();

        for (SysRole sysRole : roleList) {
            System.out.println(sysRole+"----------------");
        }

        return roleList;
    }


    // 同名验证
    @RequestMapping("/userExist")
    @ResponseBody
    public String userExist(@RequestParam String account){

        SysUser sysUser = sysUserService.getAccountExist(account);
        if (sysUser!=null){
            return "{\"exist\":\"1\"}";
        }else if ("".equals(account)){
            return "{\"exist\":\"-1\"}";
        }else {
            return "{\"exist\":\"0\"}";
        }
    }

    // 添加信息
    @RequestMapping("/add")
    public String add(SysUser sysUser){

        int add = sysUserService.toAdd(sysUser);

        if (add>0){
            return "redirect:/sys/user/list";
        }
        return "sysUser/add";
    }

    // 查看个人信息
    @RequestMapping("/view/{userid}")
    public String view(Model model,@PathVariable(name = "userid") Integer id){

        SysUser sysUser = sysUserService.findById(id);

        model.addAttribute("sysUser",sysUser);

        return "sysUser/view";
    }

    // 删除信息
    @DeleteMapping("/del/{userid}")
    @ResponseBody // 页面不跳转》》用于异步请求
    public String del(@PathVariable (name = "userid") Integer id) {

        int del = sysUserService.del(id);

        if (del > 0) {
            return "{\"delResult\":\"true\"}";
        } else if(del<0){
            return "{\"delResult\":\"false\"}";
        }else{
            return null;
        }
    }

    // 查询个人信息在修改
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,Integer uid) {

        SysUser sysUser = sysUserService.findById(uid);

        model.addAttribute("sysUser",sysUser);

        return "sysUser/upd";
    }

    // 修改个人信息
    @PostMapping("/upd")
    public String upd(SysUser sysUser) {

        int  upd = sysUserService.upd(sysUser);

        if (upd>0){
            return "redirect:/sys/user/list";
        }
        return null;
    }

    @RequestMapping("/toUpdatePwd")
    public String toUpdatePwd(){

        return "sysUser/updatePassword";
    }

    // 旧密码验证
    @PostMapping("/checkOldPwd/{oldpassword}")
    @ResponseBody
    public String checkOldPwd(@PathVariable() String oldpassword, HttpServletRequest request) {

        SysUser sysUser = (SysUser) request.getSession().getAttribute("UserSession");
        int id = sysUser.getId();

        int verify = sysUserService.queryPassword(id,oldpassword);

        if (verify>0){
            return "{\"result\":\"true\"}";
        }else if (id==-1){
            return "{\"result\":\"sessionerror\"}";
        }else {
            return "{\"result\":\"false\"}";
        }
    }


    // 修改原密码
    @PostMapping("/savePassword")
    public String savePassword(String newPassword,Integer id){

        int upd = sysUserService.savePassword(newPassword,id);

        if (upd>0){
            return "login";
        }else {
            return null;
        }
    }
}
